import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form
import CustomDialog from "@/components/HntManage/pouringOrder/WBS/index.vue";
import {
    checkSteelProtectionTestTaskData, //保存--钢保
    saveConcreteCheckAduit,//保存--回弹
    getBaseDepartmentUser,//查人
}from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "saveNum"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: "",
            isLock: false, //保存按钮是否可编辑
            dialogHeight: null,
            formData: {
                fId: "",// 当前数据fid
                aduitStatus: "",//列表页流程状态
                aduitResult: "",//审核状态  同意 驳回 撤销
                aduitNote: "",//审核意见
                aduitDates: "",//审核日期
                aduitUserId: "",//下一节点审核人id
                aduitUserName: "",//下一节点审核人
                userId: "",// 当前登陆用户ID
                userName: ""// 当前登陆用户名称
            },
            formObj: formJs.formObj, //报验信息
            btnShow: true,
            newRow: null,//当前行
            timeYMD: null, //当前日期---年月日
            timeYmdHms: null, //当前日期---年月日时分秒
            personList: [],//人下拉框
        };
    },
    components: {
        publicForm,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit();
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                this.newRow = newVal;
                // console.log(newVal.aduitStatus);
                // 1记录2复核3审核4批准5监理
                if (newVal.aduitStatus == "1") {
                    this.formObj[3].label = "下一节点记录人"
                } else if (newVal.aduitStatus == "2") {
                    this.formObj[3].label = "下一节点复核人"
                } else if (newVal.aduitStatus == "3") {
                    this.formObj[3].label = "下一节点审核人"
                } else if (newVal.aduitStatus == "4") {
                    this.formObj[3].label = "下一节点批准人"
                } else if (newVal.aduitStatus == "5") {
                    this.formObj[3].label = "下一节点监理人"
                } else if (newVal.aduitStatus == "6") {
                    this.formObj[3].disabled = true;
                    this.formObj[3].rules[0].required = false;
                }
            },
            deep: true,
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        console.log("this.userInfo", this.userInfo.companyNature,this.newRow.aduitStatus);

        if (this.newRow.aduitStatus == "6") {
            this.formObj[3].disabled = true;
            this.formObj[3].rules[0].required = false;
        } else if (this.userInfo.companyNature == "中心试验室" && this.newRow.aduitStatus == "5") {
            this.formObj[3].disabled = true;
            this.formObj[3].rules[0].required = false;
        } else {
            this.formObj[3].disabled = false;
            this.formObj[3].rules[0].required = true;
        }

        this.getData();
        setTimeout(() => {
            var that = this;
            that.getListData();
        }, 100);

    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },

        closeDialog() {
            this.$emit("close");
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        getListData() {
            this.btnShow = true;
            if (this.statusNum == 1) {
                this.formData = {
                    fId: this.newRow.fId,// 当前数据fid
                    aduitStatus: this.newRow.aduitStatus,//列表页流程状态
                    aduitResult: "同意",//审核状态  同意 驳回 撤销
                    aduitNote: "",//审核意见
                    aduitDates: this.timeYmdHms,//审批时间，默认当前
                    aduitUserId: "",//下一节点审核人id
                    aduitUserName: "",//下一节点审核人
                    userId: this.userInfo.userId,// 当前登陆用户ID
                    userName: this.userInfo.realName// 当前登陆用户名称
                };

                if (this.newRow.aduitStatus == "5") {
                    if (this.saveNum == "rebar") {
                        this.getPerson(this.newRow.fSupervisionid);
                    } else if (this.saveNum == "rebound") {
                        this.getPerson(this.newRow.fJlid);
                    }
                } else {
                    this.getPerson(this.newRow.fCompanyid);
                }
            }
        },
        // 查人
        getPerson(id) {
            var obj = {
                fCompanyid: id,
            };
            this.personList = []
            getBaseDepartmentUser(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var arr = res.data.childList[0].personList;
                        var personList = [];
                        arr.map((item) => {
                            personList.push({
                                label: item.fRealname,
                                value: item.fUserid,
                            });
                        });
                        this.formObj[3].options = personList;
                        this.personList = personList;

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })

        },

        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            // 审批状态
            if (b == "aduitResult") {
                if (data != "同意") {
                    this.formObj[3].disabled = true;
                    this.formObj[3].rules[0].required = false;
                    this.formData.aduitUserName = "";
                    this.formData.aduitUserId = "";
                } else {
                    this.formObj[3].disabled = false;
                    this.formObj[3].rules[0].required = true;
                }
            }
            // 下一节点审批人
            if (b == "aduitUserName") {
                this.personList.map(item => {
                    if (data == item.label) {
                        this.formData.aduitUserId = item.value;
                    }
                })
            }
        },
        inputFocus(index, prop) {
            // console.log("inputFocus", index, prop);
            return index, prop;
        },
        clickDialog(index, prop) {
            // console.log("clickDialog", index, prop);
            return index, prop;
        },

        // 保存
        yj_saveForm() {
            var save = {};
            save = {
                fId: this.formData.fId, // 当前数据fid
                aduitStatus: this.formData.aduitStatus,//列表页流程状态
                aduitResult: this.formData.aduitResult,//审核状态  同意 驳回 撤销
                aduitNote: this.formData.aduitNote,//审核意见
                aduitDates: this.formData.aduitDates,//审批时间，默认当前
                aduitUserId: this.formData.aduitUserId,//下一节点审核人id
                aduitUserName: this.formData.aduitUserName,//下一节点审核人
                userId: this.userInfo.userId,// 当前登陆用户ID
                userName: this.userInfo.realName// 当前登陆用户名称
            };
            if (this.$refs.publicForm.submitForm()) {
                this.isLock = true;
                if (this.saveNum == "rebar") {
                    this.save_rebar(save);
                } else if (this.saveNum == "rebound") {
                    this.save_rebound(save);
                }

            } else {
                console.log("error");
            }
        },
        // 钢保--保存
        save_rebar(data) {
            checkSteelProtectionTestTaskData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("保存成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("保存接口报错");
                    }
                })
                .catch((error) => {
                    console.log("保存接口", error);
                });
        },
        // 回弹--保存
        save_rebound(data) {
            saveConcreteCheckAduit(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("保存成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("保存接口报错");
                    }
                })
                .catch((error) => {
                    console.log("保存接口", error);
                });
        },
        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },

    },
};